package com.cw.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.cw.auth.application.convert.AuthUserDTOConverter;
import com.cw.auth.application.dto.AuthUserDTO;
import com.cw.auth.domain.entity.AuthUserBO;
import com.cw.auth.domain.service.AuthUserDomainService;
import com.cw.auth.common.entity.Result;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Resource
    private AuthUserDomainService authUserDomainService;

    /**
     * @author: 江传文
     * @date: 2024/4/27 21:19
     * 用户注册
     */
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.register.dto:{}", JSON.toJSONString(authUserDTO));
            }
            // 检查传入的字符串是否为 null、空字符串或者只包含空白字符（空格、制表符、换行符等）
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()), "用户名不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getEmail()), "邮件地址不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getPassword()), "密码不能为空");
            // (DTO -> BO)
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDtoToBO(authUserDTO);

            return Result.ok(authUserDomainService.register(authUserBO));
        } catch (Exception e) {
            log.error("UserController.register.error:{}", e.getMessage(), e);
            return Result.fail("注册用户失败");
        }
    }

    /**
     * @author: 江传文
     * @date: 2024/4/27 21:19
     * 更新用户信息
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto:{}", JSON.toJSONString(authUserDTO));
            }
            // 检查传入的字符串是否为 null、空字符串或者只包含空白字符（空格、制表符、换行符等）
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()), "用户名称不能为空");

            // (DTO -> BO)
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDtoToBO(authUserDTO);
            Boolean result = authUserDomainService.update(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新用户信息失败");
        }
    }

    /**
     * @author: 江传文
     * @date: 2024/4/27 21:19
     * 删除用户信息
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.delete.dto:{}", JSON.toJSONString(authUserDTO));
            }
            // 检查传入的字符串是否为 null、空字符串或者只包含空白字符（空格、制表符、换行符等）
            Preconditions.checkNotNull(authUserDTO.getId(), "用户id不能为空");

            // (DTO -> BO)
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDtoToBO(authUserDTO);
            Boolean result = authUserDomainService.delete(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController.update.error:{}", e.getMessage(), e);
            return Result.fail("删除用户信息失败");
        }
    }

    /**
     * @author: 江传文
     * @date: 2024/4/28 14:28
     * 用户启用/禁用
     */
    @PostMapping("/changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.changeStatus.dto:{}", JSON.toJSONString(authUserDTO));
            }
            // 检查传入的字符串是否为 null、空字符串或者只包含空白字符（空格、制表符、换行符等）
            Preconditions.checkNotNull(authUserDTO.getId(), "用户id不能为空");
            Preconditions.checkNotNull(authUserDTO.getStatus(), "用户状态不能为空");

            // (DTO -> BO)
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDtoToBO(authUserDTO);
            Boolean result = authUserDomainService.changeStatus(authUserBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController.changeStatus.error:{}", e.getMessage(), e);
            return Result.fail("用户状态变更失败");
        }
    }

    /**
     * @author: 江传文
     * @date: 2024/4/28 14:28
     * 个人信息查询
     */
    @PostMapping("/getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.getUserInfo.dto:{}", JSON.toJSONString(authUserDTO));
            }
            // 检查传入的字符串是否为 null、空字符串或者只包含空白字符（空格、制表符、换行符等）
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()), "用户名不能为空");

            // (DTO -> BO)
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDtoToBO(authUserDTO);

            AuthUserBO authUserBO1 = authUserDomainService.getUserInfo(authUserBO);
            // (BO -> DTO)
            AuthUserDTO authUserDTO1 = AuthUserDTOConverter.INSTANCE.convertBoToDTO(authUserBO1);
            return Result.ok(authUserDTO1);
        } catch (Exception e) {
            log.error("UserController.getUserInfo.error:{}", e.getMessage(), e);
            return Result.fail("查询个人信息失败");
        }
    }


    @RequestMapping("doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam("validCode") String validCode) {
        try {
            Preconditions.checkArgument(!StringUtils.isBlank(validCode), "验证码不能为空");
            return Result.ok(authUserDomainService.doLogin(validCode));
        } catch (Exception e) {
            log.error("UserController.doLogin.error:{}", e.getMessage(), e);
            return Result.fail("用户登录失败");
        }
    }

    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

}
